﻿using System.Windows.Forms;
using Repetition.Domain;
using System;

namespace Repetition.Views
{
    public partial class ProblemDifficultyImportanceView : Form
    {
        private Problem problem = new Problem();
        private Action changeHandler;
        private bool isUpdatingProblem;

        public ProblemDifficultyImportanceView(Action changeHandler)
        {
            InitializeComponent();

            easyRadioButton.Tag = Problem.DifficultyLevel.Easy;
            normalDifficultyRadioButton.Tag = Problem.DifficultyLevel.Normal;
            hardRadioButton.Tag = Problem.DifficultyLevel.Hard;

            unimportantRadioButton.Tag = Problem.ImportanceLevel.Unimportant;
            normalImportanceRadioButton.Tag = Problem.ImportanceLevel.Normal;
            importantRadioButton.Tag = Problem.ImportanceLevel.Important;

            this.changeHandler = changeHandler;
            
            // Set default property values:
            this.TopLevel = false;

            isUpdatingProblem = true;
            this.Visible = true;
            isUpdatingProblem = false;
        }

        public Problem Problem
        {
            get
            {
                return problem;
            }

            set
            {
                // Not user invoked change, so do not notify
                isUpdatingProblem = true;

                problem = value;
                UpdateDifficultyRadioButtons(problem);
                UpdateImportanceRadioButtons(problem);

                isUpdatingProblem = false;
            }
        }

        private void importanceRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            problem.Importance = (Problem.ImportanceLevel)((RadioButton)sender).Tag;
            Nofity();
        }

        private void difficultyRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            problem.Difficulty = (Problem.DifficultyLevel)((RadioButton)sender).Tag;
            Nofity();
        }

        private void Nofity()
        {
            // Do not call the event handler if the change is due to the problem being changed.
            if (!isUpdatingProblem)
                changeHandler.Invoke();
        }

        private void UpdateImportanceRadioButtons(Problem problem)
        {
            switch (problem.Importance)
            {
                case Problem.ImportanceLevel.Important:
                    importantRadioButton.Checked = true;
                    break;
                case Problem.ImportanceLevel.Normal:
                    normalImportanceRadioButton.Checked = true;
                    break;
                case Problem.ImportanceLevel.Unimportant:
                    unimportantRadioButton.Checked = true;
                    break;
            }
        }

        private void UpdateDifficultyRadioButtons(Problem problem)
        {
            switch (problem.Difficulty)
            {
                case Problem.DifficultyLevel.Easy:
                    easyRadioButton.Checked = true;
                    break;
                case Problem.DifficultyLevel.Normal:
                    normalDifficultyRadioButton.Checked = true;
                    break;
                case Problem.DifficultyLevel.Hard:
                    hardRadioButton.Checked = true;
                    break;
            }
        }
    }
}
